<script>
    // hugo-book set all bookHref links to open in a new tab, this script will change it to open in the same tab
    // detect all href attributes, if it's not an external link, set target to _self
    const links = document.querySelectorAll('a[href]');
    for (let i = 0; i < links.length; i++) {
        if (links[i].hostname === window.location.hostname) {
            links[i].target = '_self';
        }
    }

    // hijack clipboard writeText method to show a toast
    originalWriteText = navigator.clipboard.writeText;
    Object.defineProperty(navigator.clipboard, 'writeText', {
        value: async function (text) {
            const toast = document.createElement('div');
            toast.textContent = 'Copied!';
            toast.className = 'toast';
            document.body.appendChild(toast);

            requestAnimationFrame(() => {
                toast.style.opacity = '1';
            });

            setTimeout(() => {
                toast.style.opacity = '0';
                setTimeout(() => {
                    document.body.removeChild(toast);
                }, 300);
            }, 1500);

            // Replace '\' with '\' and newline
            const newText = text.replace(/\\/g, '\\\n');

            return await originalWriteText.apply(this, [newText]);
        },
        writable: true,
        configurable: true,
        enumerable: true
    });
</script>

<!--Algolia DocSearch-->
<style>
    .DocSearch-Button {
        border: 0;
        border-radius: 4px;
        font-weight: 200;
        height: 30px;
        margin: 0 0 0 0;
        padding: 0 2px;
        width: 100%;
        color: #7B7B7B;
    }

    .DocSearch-Button:hover,
    .DocSearch-Button:active,
    .DocSearch-Button:focus {
        background: rgb(235, 237, 240);
        color: #7B7B7B;
        box-shadow: inset 0 0 0 2px #646464;
        transition: background 0.15s, color 0.15s, box-shadow 0.15s;
    }

    .DocSearch-Search-Icon {
        stroke-width: 1.4;
        width: 20px;
        padding-left: 5px;
    }
    .DocSearch-Button .DocSearch-Search-Icon {
        color: #7B7B7B;
    }

    .DocSearch-Button-Placeholder {
        font-size: 0.9rem;
        padding-left: 5px;
        padding-top: 1px;
    }

    .DocSearch-Button-Keys {
        display: none;
    }
</style>
<script src="https://cdn.jsdelivr.net/npm/@docsearch/js@3"></script>
<script type="text/javascript">
    docsearch({
        appId: "BUITIDR2R4",
        apiKey: "010cbf192910433ce5b7470ca74dfd70",
        indexName: "iyear",
        container: ".book-search", // mount on original hugo-book search
        debug: false,
        insights: true,
    });
</script>
